package cn.zktr.Personnel.model.service.tanyali;

import javax.transaction.Transactional;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.zktr.Personnel.model.dao.tanyali.EmaQsbDao;
import cn.zktr.Personnel.model.dao.tanyali.EmaTmDao;
import cn.zktr.Personnel.model.mappers.tanyali.EmaQsbMapper;
import cn.zktr.Personnel.model.mappers.tanyali.EmaTsMapper;
import cn.zktr.Personnel.model.pojos.PerEmaQsb;
import cn.zktr.Personnel.model.pojos.PerEmaTm;



@Service
@Transactional(rollbackOn=Exception.class)
public class EmaQsbService {
	@Autowired
	private EmaQsbDao qsbtdao;
	@Autowired
	private EmaTmDao tmdao;
	
	@Autowired
	private EmaQsbMapper qsbmer;
	
	/**
	 * @return 题目删除
	 */
	public void deleteDX(Integer tmzid) {
		System.err.println(tmzid);
		tmdao.deleteById(tmzid);
	}
	
	
	public Iterable<PerEmaQsb> selectFebAll(){
		return qsbtdao.findAll();
	}
	
	/**
	 * @return 选择题库下拉列表更新
	 */
	public JSONArray tikuxiala(String QSB) {
		JSONArray tkdschaxun = new JSONArray();
		JSONArray tikuxiala = qsbmer.dstikuxiala("%"+QSB+"%","%"+QSB+"%","%"+QSB+"%","选择题");
		for (int i = 0; i < tikuxiala.size(); i++) {
			JSONObject json = tikuxiala.getJSONObject(i);
			String[] split = json.get("TMSUB").toString().split("#");
			String tms = split[0];
			json.put("tm", tms);
			for (int j = 1; j < split.length; j++) {
				String DA = split[j];
				json.put("DA"+j, DA);
			}
			tkdschaxun.add(json);
		};
		return tikuxiala;
	}
	
	/**
	 * @return 综合题库下拉列表更新
	 */
	public JSONArray tikuxialas(String QSBS) {
		JSONArray tkdschaxun = new JSONArray();
		JSONArray tikuxiala = qsbmer.zhtikuxiala("%"+QSBS+"%","%"+QSBS+"%","%"+QSBS+"%","综合题");
		for (int i = 0; i < tikuxiala.size(); i++) {
			JSONObject json = tikuxiala.getJSONObject(i);
			String[] split = json.get("TMSUB").toString().split("#");
			String tms = split[0];
			json.put("tm", tms);
			for (int j = 1; j < split.length; j++) {
				String DA = split[j];
				json.put("DA"+j, DA);
			}
			tkdschaxun.add(json);
		};
		return tikuxiala;
	}
	
	
	/**
	 * @return 题库模糊查询
	 */
	public JSONArray tkchaxun(String qsbbank) {
		return qsbmer.tikuchaxun("%"+qsbbank+"%");
	}
	
	/**
	 * @return 题库单选模糊查询
	 */
	public JSONArray tkdschaxun(String tmsub) {
		return qsbmer.tikudanshunchaxun("选择题","%"+tmsub+"%","%"+tmsub+"%","%"+tmsub+"%","%"+tmsub+"%");
	}
	
	/**
	 * @return 题库综合模糊查询
	 */
	public JSONArray tkzhchaxun(String tmsub) {
		return qsbmer.tikuzonghechaxun("综合题","%"+tmsub+"%","%"+tmsub+"%","%"+tmsub+"%","%"+tmsub+"%");
	}
	/**
	 * @param tmid
	 * @param xxa
	 * @param xxb
	 * @param xxc
	 * @param xxd
	 * @param daan
	 * @param timu
	 * 选择题修改功能
	 */
	public void xztxiugai(Integer tmid,String xxa,String xxb,String xxc,String xxd,String daan,String timu) {
		 PerEmaTm tm = tmdao.findById(tmid).get();
		String str=timu+"#"+xxa+"#"+xxb+"#"+xxc+"#"+xxd;
		tm.setTmsub(str);
		tm.setTmbda(daan);
		tmdao.save(tm);
		
	}
	
	/**
	 * @param tmid
	 * @param daan
	 * @param timu
	 * 综合题修改功能
	 */
	public void zhtxiugai(Integer tmid,String daan,String timu) {
		 PerEmaTm tm = tmdao.findById(tmid).get();
		 String str=timu+"#"+daan;
		 tm.setTmsub(str);
		 tmdao.save(tm);
		
	}
	
	/**
	 * @param tmid
	 * @param daan
	 * @param timu
	 * 信息反馈修改/编辑功能
	 */
	public void xxfkxiugai(Integer bianhao,String kaikao,String jieshu,String shichang,String didian) {
		 PerEmaQsb qsb = qsbtdao.findById(bianhao).get();
		 qsbtdao.save(qsb);
		
	}
}
